﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.NganhHocService;
using eTMS.Controllers;

namespace eTMS.UserControls
{
    public partial class ChonNganhHoc : eTMS.Controls.UserControlBase
    {
        public ChonNganhHoc()
        {
            InitializeComponent();
        }
        public string Title
        {
            get
            {
                return groupBox1.Text;
            }
            set
            {
                groupBox1.Text = value;
            }
        }

        private bool IsInternalChanged = false;
        public void SetDefaultNganhHoc(string MaNganh)
        {
            NganhHocEntity nganhHocEntity = NganhHocController.FindNganhHocByMaNganhHoc(MaNganh);
            if (nganhHocEntity != null)
            {
                comboBoxKhoa1.SelectedValue = nganhHocEntity.Khoa_ID;
                comboBoxNganhHoc1.SelectedValue = nganhHocEntity.NganhHoc_ID;
            }
        }
        public int GetSelectedNganhHoc_ID()
        {
            if (comboBoxNganhHoc1.SelectedIndex < 0) return -1;
            return (int) comboBoxNganhHoc1.SelectedValue;
        }
        public NganhHocEntity GetSelectedNganhHoc()
        {
            return (NganhHocEntity)comboBoxNganhHoc1.SelectedItem;
        }
        public void SetDefaultNganhHoc(int NganhHoc_ID)
        {
            NganhHocEntity nganhHocEntity = NganhHocController.FindNganhHocByNganhHoc_ID(NganhHoc_ID);
            comboBoxKhoa1.SelectedValue = nganhHocEntity.Khoa_ID;
            comboBoxNganhHoc1.SelectedValue = nganhHocEntity.NganhHoc_ID;
        }
        private void comboBoxKhoa1_SelectedValueChanged(object sender, EventArgs e)
        {
            IsInternalChanged = true;
            if (comboBoxKhoa1.SelectedIndex > 0)
            {
                comboBoxNganhHoc1.AddSearchValues("Khoa_ID", comboBoxKhoa1.SelectedValue);
            }
            else
            {
                comboBoxNganhHoc1.ClearSearchValue();
            }
            comboBoxNganhHoc1.BindData();
            IsInternalChanged = false;
        }
        private void ChonNganhHoc_Load(object sender, EventArgs e)
        {
            if(DesignMode)return;
            KhoaController.LoadAllKhoa(false);
            NganhHocController.LoadAllNganhHoc(false);
            comboBoxKhoa1.BindData();
            comboBoxNganhHoc1.BindData();
        }
        public event EventHandler OnChangedSelection;

        private void comboBoxNganhHoc1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (IsInternalChanged)return;
            if(OnChangedSelection!=null)
            {
                OnChangedSelection(sender,new EventArgs());
            }
        }

    }
}
